import { Channels } from '/imports/models/Channels';

const channelFadeCard = `
<div class="col-xl-3 col-lg-4 col-md-6 card-field channel-fade-card">
  <div class="card card-fade"></div>
</div>
`;

Template.home_channels_created.onCreated(function() {
  this.infiniteScroll({
    query: {
      'owner.id': Meteor.userId()
    },
    publication: 'get.created.channels',
    countPublication: 'get.created.channels.counts'
  });
});

Template.home_channels_created.helpers({
  fadeCard: [1,2,3],

  results: function() {
    const channels = Channels.find({'owner.id': Meteor.userId()}, {
      limit: Template.instance().getLimit(),
      sort: {
        createdAt: -1
      }
    });

    if (channels.count() === 0) {
      $(channelFadeCard).appendTo('#created-channels-infinite-field');
    }

    $('.channel-fade-card').remove();

    if (channels.count() < 3) {
      let num = 3 - channels.count();
      for (let i = 0; i < num; i++) {
        $(channelFadeCard).appendTo('#created-channels-infinite-field');
      }
    }

    return channels;
  }
});